require(["config"],function () {
    require(["jquery","jquery.cookie"],function () {
        $(function () {
            $(".top").load("shareHeader.html");
            $(".foot").load("shareFooter.html");
            //ajax
            $.ajax({
                url:"../../server/goodsById.php"
            }).then(function (res) {
                var resObj=JSON.parse(res)
                if(resObj.status==1){
                    //渲染页面
                        //1放大镜；
                    var imgObj=resObj.data.imgLists;
                    $(".bigImage img")[0].src=$(".smallImage img")[0].src=imgObj[0];
                    $(".imageLists>ol img").each(function (index,value) {
                        value.src=imgObj[index];
                    })
                        //2
                    $(".gi_content>.gc_msg>img")[0].src=resObj.data.introduction;
                    $(".cb_middle_center>h2").html(resObj.data.title)
                    $(".goosDis>em").html(resObj.data.pid);
                    $(".goodsSale strong").html("$"+resObj.data.price);
                    $(".fastSale>em").html(resObj.data.dis);
                    $(".cb_top>ul>li:last-child").html(resObj.data.title);

                    //保存至按钮
                    console.log(resObj.data);
                    delete(resObj.data.dis);
                    delete(resObj.data.imgLists);
                    delete(resObj.data.introduction);
                    delete(resObj.data.arguments);
                    $(".butIt>a").eq(1).attr("data-buyMsg",JSON.stringify(resObj.data));
                }
            })
            //tab切换
            $(".gi_head>ul>li").on("click",function () {
                $(".gi_content>div").eq($(this).index()).addClass("current").siblings().removeClass("current")
            })

            //放大镜
            var $bigImg=$(".bigImage");
            var $bigArea=$(".bigArea");
            var $smallImg=$(".smallImage");
            var $smallAreaWidth=($bigArea.width()/$bigImg.width())*$smallImg.width();
            var scale=$bigImg.width()/$bigArea.width();
            $(".smallArea").css({width:$smallAreaWidth,height:$smallAreaWidth});
            $(".smallImage").on("mouseenter",function (evt) {
                $(".smallArea").show();
                $(".bigArea").show();
                $(".smallImage").on("mousemove",function (evt) {
                    var moveX=evt.pageX-$(this).offset().left-$(".smallArea").width()/2;
                    var moveY=evt.pageY-$(this).offset().top-$(".smallArea").width()/2;

                    if(moveX<=0){
                        moveX=0;
                    }
                    if(moveX>=$smallImg.width()-$smallAreaWidth){
                        moveX=$smallImg.width()-$smallAreaWidth;
                    }
                    if(moveY<=0){
                        moveY=0;
                    }
                    if(moveY>=$smallImg.width()-$smallAreaWidth){
                        moveY=$smallImg.width()-$smallAreaWidth;
                    }
                    var bigMoveX=-moveX*scale;
                    var bigMoveY=-moveY*scale;
                    $(".smallArea").css({left:moveX,top:moveY});
                    $(".bigImage").css({left:bigMoveX,top:bigMoveY})
                })
            }).on("mouseleave",function () {
                $(".smallArea").hide();
                $(".bigArea").hide();
                $(document).off("mousemove","mouseleave");

            })
            //点击换图
            $(".imageLists>ol>li>img").on("click",function () {
                $(".smallImage>.smallImage_box>img")[0].src=this.src;
                $(".bigImage>img")[0].src=this.src;
            })

            //商品数量增减
            $(".number>button").on("click",function () {
                var $num=$(".number>input").val()-0;
                if($(this).index()==0){
                    if($num>1){
                        $num--;
                        $(".number>input").val($num);
                    }
                }else{
                    $num++;
                    $(".number>input").val($num)
                }
            })
            
            //立即购买---------------------------------未完成
            $(".butIt>a").eq(0).on("click",function () {
                window.location.href="shoppingCart.html";
            })
            //加入购物车-------------------------------in
            $(".butIt>a").eq(1).on("click",function () {
                var myNum=$(".number>input").val()-0;
                var goodMsg=$(this).data().buymsg; //{"abc":123,"bb":"23"}
                goodMsg["num"]=myNum;
                var arrCookie=JSON.parse($.cookie("cart")||"[]");
                if(sessionStorage.getItem("ifLogin")){
                    console.log("iflogin");
                    //已经登录
                    $.ajax({
                        url:"../../server/getGoods.php",
                        data:goodMsg,
                        type:"post"
                    }).then(function (res) {
                        console.log(res);
                        if(res.status==1){
                            console.log(res);
                            window.location.href="shoppingCart.html"
                        }
                    })
                }else{
                    var bflag=true;
                    arrCookie.forEach(function (val,index) {
                        if(val.pid==goodMsg.pid){
                            val.num=val.num-0+goodMsg.num;
                            console.log(val.num);
                            bflag=false;
                        }
                    })
                    if(bflag){
                        arrCookie.push(goodMsg);
                    }
                    $.cookie("cart",JSON.stringify(arrCookie));
                }
                alert("添加成功");
            })
        })
    })
})